package iwb.reference.cacheaop;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

import java.util.*;
import java.util.stream.Stream;


public class Utils {

    public static final String NULL_TEXT = " ";
    public static final String SEPARATOR = "_";

    /**
     * getCacheKey.
     * @param methodName methodName
     * @param args args
     * @return String
     */
    public static String getCacheKey(String methodName, Object[] args) {
        Joiner joiner = Joiner.on(SEPARATOR).useForNull(NULL_TEXT);
        LinkedList joinStr = Lists.newLinkedList();
        joinStr.addFirst(methodName);
        Stream.of(args).forEach(joinStr::add);
        return joiner.join(joinStr);
    }

    /**
     * getCacheKey.
     *
     * @param methodName String
     * @param className String
     * @param args Object[]
     *
     * @return String
     */
    public static String getCacheKey(String methodName,String className, Object[] args) {
        Joiner joiner = Joiner.on(SEPARATOR).useForNull(NULL_TEXT);
        LinkedList joinStr = Lists.newLinkedList();
        joinStr.addFirst(className);
        joinStr.add(methodName);
        Stream.of(args).forEach(joinStr::add);
        return joiner.join(joinStr);
    }

    /**
     * main.
     *
     * @param args String[]
     *
     */
    public static void main(String[] args) {
        Object[] asdf = new Object[4];
        List<String> sd= new ArrayList<>();
        sd.add("sd1");
        sd.add("sd2");
        Map<String,String> mm = new HashMap<>();
        mm.put("sss","eeee");
        asdf[0] = "aaaa";
        asdf[1] = sd;
        asdf[2] = null;
        asdf[3] = mm;
        System.out.println(Utils.getCacheKey("methodName",asdf));
    }
}
